<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-button type="warning" icon="el-icon-refresh-right" style="margin-left: 10px" @click="reset"></el-button>
      <el-button type="success" @click="processSleepRecordStart">开启健康信息记录</el-button>
      <el-button type="danger" @click="processSleepRecordStop">关闭健康信息记录</el-button>
    </div>

    <div class="container" style="padding:0 0;">
      <div ref="chart_1" style="width: 50%; height: 500px; background-color:rgba(10, 2, 39, 0.796);"></div>
    </div>
    <div>
    </div>
    <!--    睡眠信息表-->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="sleepId" label="id"></el-table-column>
        <el-table-column prop="duration" label="睡眠时长(小时)"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>

import * as echarts from 'echarts';
import request from "@/utils/request";
import {Client} from '@stomp/stompjs';
import SockJS from 'sockjs-client';

export default {
  name: 'sleep',
  data() {
    return {
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {},
      tableData: [],
      params: {
        userid: 0,
        beginTime: '',
        endTime: '',
      },
      chart_1: null,
      stompClient: null,
    }
  },
  created() {

  },
  mounted() {
    this.fetchData();
    this.chart_1 = echarts.init(this.$refs.chart_1);
    this.initChart();
    this.connect();
  },
  methods: {
    fetchData() {
      request.get("/sleep/search/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.tableData = res.data
          this.$message.success("获取睡眠信息成功")
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    //清空
    reset() {
      this.tableData = [];
    },
    processSleepRecordStart() {
      request.post("/process/start/sleepRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("开启成功");
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
        console.error('Error starting process:', error);
      })
    },
    processSleepRecordStop() {
      request.post("/process/stop/sleepRecord/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.$message.success("关闭成功");
          this.fetchData();
        } else {
          this.$message.error(res.msg)
        }
      }).catch(error => {
        console.error('Error stopping process:', error);
      })
    },
    initChart() {
      this.initChart_1()
    },
    initChart_1() {
      const xAxisData = this.tableData.map(item => item.sleepId);
      const sleepDuration = this.tableData.map(item => ({
        value: [item.sleepId, item.duration],
        name: item.sleepId
      }));
      var option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: "#dddc6b",
            },
          },
        },
        title: {
          text: "睡眠时长(小时)",
          left: "left",
          textStyle: {
            color: "rgba(11, 240, 125, 0.796)",
            fontSize: "15",
          },
        },
        grid: {
          left: "5%",
          top: "10%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        xAxis: {
          type: 'value',
          data: xAxisData,
          boundaryGap: false,
          axisLabel: {
            textStyle: {
              color: "rgba(255,255,255,.5)",
              fontSize: 10,
            },
          },
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,.2)",
            },
          },
          splitLine: {
            lineStyle: {
              color: "rgba(255,255,255,.1)",
            },
          }
        },
        yAxis: [
          {
            type: 'value',
            name: '睡眠时长(小时)',
            axisTick: {show: false},
            axisLabel: {
              textStyle: {
                color: "rgba(255,255,255,.5)",
                fontSize: 10,
              },
              // formatter: '{value} KM',
            },
            axisLine: {
              lineStyle: {
                color: "rgba(255,255,255,.1)",
              },
            },
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,.1)",
              },
            },
          },
        ],
        series: [
          {
            name: '睡眠时长(小时)',
            type: 'line',
            data: sleepDuration,
            yAxisIndex: 0,

            // 平滑曲线 或 折线
            // smooth: true,
            // 面接图
            areaStyle: {},
            symbol: "circle",
            symbolSize: 5,
            lineStyle: {
              normal: {
                // color: '#0184d5',
                width: 2,
              },
            },
          }
        ]
      };
      this.chart_1.setOption(option);
    }
  },
  watch: {
    tableData(newValue, oldValue) {
      console.log(newValue)
      this.initChart();
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: space-between;
  width: 100%;
  padding: 20px;
}

.left-panel, .right-panel {
  width: 45%; /* 根据需要调整 */
  text-align: center;
  border: 1px solid #ccc; /* 可选，仅为了更清晰地看到边界 */
  padding: 20px;
  box-sizing: border-box;
}
</style>
